package equals;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-07-05
 * Time: 15:29
 */
class Person{
    public String id;

    public Person(String id) {
        this.id = id;
    }

    public boolean equals(Object obj) {
        if(obj == null){
            return false;
        }else if(obj == this){
            return true;
        }else if(!(obj instanceof Person)){
            return false;
        }
        Person tmp =(Person)obj;//将父类object引用转换为Person引用，然后赋值给中间Person引用变量tmp
        return this.id.equals(tmp.id);// this-调用当前方法对象的引用 .访问对象里的成员变量id
        // .equals-已经被编译器底层重写过的比较字符串的方法 (tmp.id)访问中间变量中的id成员变量
    }
}
class Student{

}
public class Test {
    public static void main(String[] args) {
        Person person = new Person("123");
        Student student = new Student();
        Person person3 = person;
        Person person1 = new Person("123");
        Person person2 = null;
        System.out.println(person==person1);//输出结果为false ==两边引用类型比较的是地址
        System.out.println(person.equals(person3));//输出结果为false ==两边引用类型比较的是地址
        System.out.println(person.equals(student));

        System.out.println(person.equals(person1));//输出结果为true 已经将Object中的equals方法重写

    }
}
